package org.vectrics.recruiter.struts;


import org.apache.log4j.*;
import org.vectrics.SystemException;
import org.vectrics.common.util.DateSync;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.user.RecruitUserManager;
import org.vectrics.recruiter.user.Resume;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;

import javax.servlet.jsp.JspException;


public class ShowTextResumeAction extends VectricsStrutsAction {
    private static Category log = Category.getInstance(ShowTextResumeAction.class);

	protected Person getPersonWithResume(Request request) {
    	String userId = request.getParameter("userId");
		Long personId = new Long(userId);
		Person person = CoreServiceLocator.getPersonService().findPersonById(personId);
		return(person);
	}

    @Override
	public String doExecute(VectricsActionData actionData) {
        log.info("execute() called");
        try {
        	String resumeId = actionData.getRequest().getParameter("resumeId");
        	Person personWithResume = this.getPersonWithResume(actionData.getRequest());
        	RecruitUserManager recruitUserMgr = new RecruitUserManager();
	        Resume resume = null;
	        if (resumeId != null) {
	        	resume = recruitUserMgr.findResume(new Long(resumeId));
	        }
	        
	
			ResumeForm resumeForm = null;
			if (actionData.getForm() == null) {        
	        	resumeForm = new ResumeForm();
	        	actionData.setForm(resumeForm);
				if ("request".equals(actionData.getMapping().getScope())) {
					actionData.getRequest().setAttribute(actionData.getMapping().getAttribute(), resumeForm);
				}
				else {
					actionData.getRequest().setSessionAttribute(actionData.getMapping().getAttribute(), resumeForm);
				}
			}
			else {
				resumeForm = (ResumeForm)actionData.getForm();			
			}
	
	        
        	//Person user = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(ThreadProperties.getDomain(), 
        	//		actionData.getRequest());
        	resumeForm.setResumeId(resumeId);
        	resumeForm.setUserId(personWithResume.getId().toString());

	        resumeForm.setPersonName(personWithResume.getName());
        	try {
        		if (resume != null) {
                	resumeForm.setName(resume.getName());
        			
	        		String text = resume.getTextResume();
	        		if (log.isDebugEnabled()) {
	        			if (text.length() < 500) {
	        				log.debug("execute() - resume text = " + text);
	        			} else {
	        				log.debug("execute() - resume text = " + text.substring(0, 400) + "...");
	        			}
	        			log.debug("resumeId = " + resumeId);
	        		}
	        		resumeForm.setResumeText(text);
        		} else {
        			String resumeName = this.getPhrase("phrase.resume.name.default", actionData.getLocale());
        			resumeName += " " + CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(actionData.getLocale()).format(DateSync.getCurrentTime());
        			resumeForm.setName(resumeName);
        		}
        	} catch (Exception ve) {
        		log.error("Exception getting resume text", ve);
        		throw new JspException("Exception getting resume text", ve);
        	}
        	
        }
        catch (Exception ex) {
        	log.error("Exception finding auth user", ex);
        	throw new SystemException("Exception finding auth user", ex);
        }
        log.info("execute() - finished.");
        
        return("success");
    }
    
}
